package com.yingli.myapplication.test;

import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ListView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.NotificationCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.yingli.myapplication.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class ListViewActivity extends AppCompatActivity {
    private RecyclerView recyclerViews; // 初始化列表
    // 一个类 ：继承： RecyclerView.Adapter<Living>
    private LivingAdapter livingAdapter;

    public Button Add;
    private ListView listView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activty_list);
        recyclerViews = findViewById(R.id.a_list); // 初始化id

        Add = findViewById(R.id.add);

        List<Entity> entity = new ArrayList<>();
        for (int i = 0; i <= 20 ; i++){
            Entity entity1 = new Entity();
            entity1.setName("名字：小明"+i);
            entity1.setContent("内容：今天天气好好哦,今天天气好好哦,今天天气好好哦,今天天气好好哦,今天天气好好哦,今天天气好好哦,今天天气好好哦,今天天气好好哦,");
            entity.add(entity1);
        }
        livingAdapter = new LivingAdapter(this,entity);
        recyclerViews.setLayoutManager(new LinearLayoutManager(this,RecyclerView.VERTICAL,false));
        recyclerViews.setAdapter(livingAdapter);
        Add.setOnClickListener(v -> {
            Entity entity2 = new Entity();
            entity2.setName("新添加的名字: 啊札");
            entity2.setContent("内容：还好吧！今天的天气不冷不热的，没风没雨的");
            Random random = new Random();
            int p = random.nextInt(entity.size()-1);
            entity.add(p,entity2);
            recyclerViews.scrollToPosition(p);
            livingAdapter.notifyItemInserted(p);//通知

            //长按提示是否删除，
//            listView.setOnItemLongClickListener((parent, view, position, id) -> {
//                entity.remove(entity);
//                return true;
//            });

        });
    }
    private NotificationManager notificationManager;//管理类
    private String channelId = "notification1";
    public void notifyChanged(String message){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
            Notification notification = new NotificationCompat.Builder(this,channelId)
                    .setContentText(message)
                    .setContentTitle("这里是标题")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_background))
                    .build();
            //创建通知渠道
            NotificationChannel channel = new NotificationChannel(channelId,"测试通知",NotificationManager.IMPORTANCE_DEFAULT);
            notificationManager.createNotificationChannel(channel);
            notificationManager.notify(100,notification);
        }else {
            Notification notification =
                    new NotificationCompat.Builder(this,channelId)
                    .setContentText(message)
                    .setContentInfo("这里是标题")
                    .setSmallIcon(R.drawable.ic_launcher_background)
                    .build();
            notificationManager.notify(100,notification);
        }
    }
}
